package com.sq.subject.domain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSON;
import com.sq.subject.domain.bo.SubjectCategoryBO;
import com.sq.subject.domain.bo.SubjectLabelBO;
import com.sq.subject.domain.service.SubjectLabelDomainService;
import com.sq.subject.infra.basic.entity.SubjectLabel;
import com.sq.subject.infra.basic.entity.SubjectMapping;
import com.sq.subject.infra.basic.mapper.SubjectLabelDao;
import com.sq.subject.infra.basic.mapper.SubjectMappingDao;
import com.sq.subject.infra.basic.service.SubjectLabelService;
import com.sq.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author quan
 */
@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelDao subjectLabelDao;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectLabelService subjectLabelService;

    @Override
    public void add(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.add.bo：{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = BeanUtil.copyProperties(subjectLabelBO, SubjectLabel.class);
        subjectLabelService.insert(subjectLabel);
    }

    @Override
    public void update(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.update.bo：{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = BeanUtil.copyProperties(subjectLabelBO, SubjectLabel.class);
        subjectLabelService.update(subjectLabel);
    }

    @Override
    public void delete(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.delete.bo：{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = BeanUtil.copyProperties(subjectLabelBO, SubjectLabel.class);
        subjectLabelService.deleteById(subjectLabel.getId());
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.queryLabelByCategoryId.bo：{}", JSON.toJSONString(subjectLabelBO));
        }
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        List<SubjectMapping> subjectMappingList = subjectMappingService.queryByCondition(subjectMapping);
        if(CollUtil.isEmpty(subjectMappingList)){
            return new ArrayList<>();
        }
        List<Long> labelIds = subjectMappingList.stream().map(SubjectMapping::getLabelId).toList();
        List<SubjectLabel> subjectLabelList = subjectLabelService.batchQueryById(labelIds);
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.queryLabelByCategoryId.subjectLabelList：{}", JSON.toJSONString(subjectLabelList));
        }
        return BeanUtil.copyToList(subjectLabelList, SubjectLabelBO.class);
    }
}
